''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>
Public Class LevelPicker
    Inherits Microsoft.Xna.Framework.DrawableGameComponent
    Private mQuad As VertexPositionTexture()
    Private Parent As Game1
    Private mCheck As Texture2D
    Private mNormalLevItem As Texture2D
    Private mLockedLevItem As Texture2D
    Private mEffect As BasicEffect
    Private SpriteBatch As SpriteBatch
    Private mloaded As Boolean = False
    Private mTCord As Single = 0
    Private mBacker As Texture2D
    Private Levels() As LPItem
    Private mFont As SpriteFont
    Private SaveGame As savegame

    Public Event LevelSelected(ByVal Level As Byte)
    Public Sub New(ByVal game As Game)
        MyBase.New(game)
        Parent = CType(game, Game1)
    End Sub

    Public Overrides Sub Initialize()
        MyBase.Initialize()
    End Sub

    Public Overrides Sub Update(ByVal gameTime As GameTime)
        ' Now move the super imposed quad
        If (mloaded) Then
            mTCord = CSng(mTCord + 0.05)
            If (mTCord >= 1) Then
                mTCord = 0
            End If
            If (Not mQuad Is Nothing) Then
                For intI As Integer = 0 To mQuad.Count - 1
                    If (mQuad(intI).TextureCoordinate.X >= 15) Then
                        mQuad(intI).TextureCoordinate.X = 15 + mTCord
                    Else
                        mQuad(intI).TextureCoordinate.X = mTCord
                    End If
                Next
            End If

            Dim touchCollection As TouchCollection = TouchPanel.GetState()
            For intLevels As Integer = 0 To Levels.Count - 1
                If (Levels(intLevels).CheckColision(touchCollection)) Then
                    RaiseEvent LevelSelected(CByte(intLevels + 1))
                    Exit For
                End If
            Next
        End If
        MyBase.Update(gameTime)
    End Sub

    Protected Overrides Sub UnloadContent()
        mloaded = False
        mCheck = Nothing
        SpriteBatch = Nothing
        mQuad = Nothing
        mEffect = Nothing
        mBacker = Nothing
        mNormalLevItem = Nothing
        mLockedLevItem = Nothing
        mFont = Nothing
        If (Not Levels Is Nothing) Then
            For intLevel = 0 To Levels.Count - 1
                Levels(intLevel) = Nothing
            Next
        End If
        Levels = Nothing
        MyBase.UnloadContent()
    End Sub

    Protected Overrides Sub LoadContent()
        mQuad = New VertexPositionTexture(3) {}
        mQuad(0) = New VertexPositionTexture()
        mQuad(0).Position = New Vector3(1, -1, 1)
        mQuad(0).TextureCoordinate = New Vector2(15, 0)
        mQuad(1) = New VertexPositionTexture()
        mQuad(1).Position = New Vector3(1, 1, 1)
        mQuad(1).TextureCoordinate = New Vector2(15, 15)
        mQuad(2) = New VertexPositionTexture()
        mQuad(2).Position = New Vector3(-1, -1, 1)
        mQuad(2).TextureCoordinate = New Vector2(0, 0)
        mQuad(3) = New VertexPositionTexture()
        mQuad(3).Position = New Vector3(-1, 1, 1)
        mQuad(3).TextureCoordinate = New Vector2(0, 15)

        SaveGame = New savegame
        mFont = Game.Content.Load(Of SpriteFont)("motorwerklargest")
        mCheck = Game.Content.Load(Of Texture2D)("checks")
        mBacker = Game.Content.Load(Of Texture2D)("lpicker")
        mNormalLevItem = Game.Content.Load(Of Texture2D)("donelevel")
        mLockedLevItem = Game.Content.Load(Of Texture2D)("lockedlevel")
        ' Load the level images
        Levels = New LPItem(11) {}
        For intLevel As Integer = 0 To Levels.Count - 1
            Levels(intLevel) = New LPItem(intLevel + 1, mNormalLevItem, mLockedLevItem, mFont, SaveGame)
        Next
        mEffect = New BasicEffect(GraphicsDevice)
        mEffect.TextureEnabled = True
        mEffect.World = Matrix.Identity
        mEffect.View = Matrix.CreateLookAt(Vector3.Zero, New Vector3(0, 0, 1), Vector3.Up)
        mEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.1, 10)
        mEffect.Texture = mCheck
        mEffect.TextureEnabled = True
        SpriteBatch = New SpriteBatch(GraphicsDevice)
        mloaded = True
        MyBase.LoadContent()
    End Sub
    Public Overrides Sub Draw(gameTime As Microsoft.Xna.Framework.GameTime)
        If (mloaded) Then
            mEffect.CurrentTechnique.Passes(0).Apply()
            Dim States As RenderStates = New RenderStates
            RecordRenderState(States, GraphicsDevice)
            GraphicsDevice.BlendState = BlendState.AlphaBlend
            GraphicsDevice.DepthStencilState = DepthStencilState.None
            Game.GraphicsDevice.DrawUserPrimitives(Of VertexPositionTexture)(PrimitiveType.TriangleStrip, mQuad, 0, 2)

            SpriteBatch.Begin()
            SpriteBatch.Draw(mBacker, New Vector2(CInt(GraphicsDevice.Viewport.Width / 2.0 - mBacker.Width / 2.0), CInt(GraphicsDevice.Viewport.Height / 2.0 - mBacker.Height / 2.0)), Color.White)
            For intLevel As Integer = 0 To Levels.Count - 1
                Levels(intLevel).Draw(SpriteBatch)
            Next
            SpriteBatch.End()
            RestoreRenderState(States, GraphicsDevice)
        End If
        MyBase.Draw(gameTime)
    End Sub

End Class
